<template>
  <d2-container>
    <el-form :model="FormSearch" status-icon label-width="100px" class="demo-ruleForm">
      <el-button type="primary" @click="back()" style="margin-bottom: 10px;">返回</el-button>
      <!-- <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item label="条件查找" prop="good">
            <el-input
              type="text"
              v-model="FormSearch.good"
              placeholder="请输入缴费名称"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" @click="searchClick()">查询</el-button>
          <el-button @click="resetForm()">重置</el-button>
        </el-col>
      </el-row> -->
    </el-form>
    <!-- 数据表格 -->
    <d2-crud
      ref="d2Crud"
      edit-title="编辑"
      add-title="添加批次"
      :columns="columns"
      :data="data"
      :edit-template="editTemplate"
      :add-template="addTemplate"
      :add-rules="addRules"
      :form-options="formOptions"
      :pagination="page"
      :loading="loading"
      @dialog-open="handleDialogOpen"
      @row-edit="handleRowEdit"
      @row-add="handleRowAdd"
      @dialog-cancel="handleDialogCancel"
      @custom-emit-1="createBatch"
      @custom-emit-2="delGood"
      @pagination-current-change="paginationCurrentChange"
    >
    <!-- <el-button slot="header" style="margin-bottom: 5px;" @click="addRow">添加批次</el-button> -->
    </d2-crud>
  </d2-container>
</template>

<script>
import { orderKsTjMx, addItemBatch, setItemBatch, deleteItemBatch } from "@/api/accountAdmin/index";
import util from "@/libs/util.js";

export default {
  data() {
    return {
      loading: false,
      // 搜索数据
      FormSearch: {
        good: ""
      },
      // 字段名称
      columns: [
        {
          title: "订单id",
          key: "oid",
          align: "center"
        },
        {
          title: "商品名称",
          key: "g_name",
          align: "center"
        },
        {
          title: "数量",
          key: "sum",
          align: "center"
        },
      ],
      // 列表数据
      data: [],
      // 操作列数据
      rowHandle: {
        align: "center",
        width: 280,
        custom: [
          {
            text: "删除",
            type: "danger",
            size: "mini",
            emit: "custom-emit-2"
          },
        ],
        edit: {
          icon: "el-icon-edit",
          text: "编辑",
          size: "small"
        }
      },
      addTemplate: {
        /* batch_name: {
          title: "批次名称",
          value: ""
        }, */
        batchYear: {
          title: "批次年份",
          value: "",
          component: {
            name: 'el-date-picker',
            type: 'year',
            valueFormat: "yyyy",
            span: 24
          }
        },
        type: {
          title: "生成单位",
          value: "3",
          component: {
            name: 'el-select',
            options: [
              { value: '3', label: '按季度' },
              { value: '1', label: '按年' },
              { value: '12', label: '按月' },
            ],
            span: 24
          }
        },
      },
      addRules: {
        batchYear: [ { required: true, message: '请输入批次年份', trigger: 'blur' } ],
        type: [ { required: true, message: '请输入批次年份', trigger: 'blur' } ],
      },
      editTemplate: {
        batch_name: {
          title: "批次名称",
          value: ""
        },
        /* batch_year: {
          title: "批次年份",
          value: "",
          component: {
            name: 'el-date-picker',
            type: 'year',
            valueFormat: 'yyyy',
            span: 24
          }
        }, */
        status1: {
          title: "批次状态",
          value: '',
          component: {
            name: 'el-select',
            options: [
              { value: '1', label: '显示' },
              { value: '0', label: '隐藏' },
            ],
            span: 24
          }
        },
      },
      formOptions: {
        labelWidth: "80px",
        labelPosition: "left",
        saveLoading: false
      },
      // 分页数据
      page: {
        total: 0,
        pageIndex: 1,
        pageSize: 10
      }
    };
  },
  created() {
    let params = {
      page: this.page.pageIndex,
      limit: this.page.pageSize
    };
    this.init(params);
  },
  methods: {
    // 初始化身体数据列表
    init(params) {
      params.otype = this.$route.query.otype
      params.address = this.$route.query.address
      params.shid = util.cookies.get('uuid')
      this.loading = true
      orderKsTjMx(params)
        .then(res => {
          if (res.success) {
            res.rows.map(item => {
              if (item.g_time == 1) {
                item.g_time1 = '早'
              } else if (item.g_time == 2) {
                item.g_time1 = '中'
              } else if (item.g_time == 3) {
                item.g_time1 = '晚'
              }
            })
            this.data = res.rows;
            this.page.total = res.total;
          }
          this.loading = false
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err);
        });
    },

    // 普通的新增
    addRow() {
      this.$refs.d2Crud.showDialog({
        mode: "add"
      });
    },

    // 新增成功
    handleRowAdd(row, done) {
      this.formOptions.saveLoading = true;
      let params = row;
      console.log(row)
      params.itemId = this.$route.query.itemId
      addItemBatch(params)
        .then(res => {
          if (res.success) {
            this.$message({
              message: "添加成功",
              type: "success"
            });
          } else {
            this.$message({
              message: "请勿重复生成!",
              type: "warning"
            });
          }

          // done可以传入一个对象来修改提交的某个字段
          done();
          // 保存按钮的loading状态
          this.formOptions.saveLoading = false;
          // 重新加载列表
          let params = {
            page: this.page.pageIndex,
            limit: this.page.pageSize,
          };
          this.init(params);
        })
        .catch(err => {
          this.$message.error(err);
        });
    },

    // 返回上一级
    back() {
      this.$router.back(-1)
    },

    // 分页数改变，调用
    paginationCurrentChange(pageIndex) {
      let params = {
        limit: this.page.pageSize,
        name: this.FormSearch.good
      };
      params.page = this.page.pageIndex = pageIndex;
      this.init(params);
    },

    // 条件查询搜索
    searchClick() {
      let params = {
        page: 1,
        limit: 10
      };
      params.name = this.FormSearch.good;
      this.init(params);
    },

    // 重置查询
    resetForm() {
      this.FormSearch.good = "";
      let params = {
        page: 1,
        limit: 10
      };
      this.init(params);
    },

    // 点击编辑时 mode 为编辑或者添加，row为点击这一行数据
    handleDialogOpen({ mode, row }) {
    },

    // 编辑提交
    handleRowEdit({ index, row }, done) {
      // 保存按钮的loading状态
      this.formOptions.saveLoading = true;
      let params = {};
      params.id = row.id;
      params.batchName = row.batch_name;
      // params.batchYear = row.batch_year;
      if (!isNaN(row.status1)) {
        params.status = row.status1
      } else {
        params.status = row.status
      }
      setItemBatch(params)
        .then(res => {
          if (res.success) {
            this.$message({
              message: "编辑成功",
              type: "success"
            });
          }
          // done可以传入一个对象来修改提交的某个字段
          done();
          // 保存按钮的loading状态
          this.formOptions.saveLoading = false;
          // 重新加载列表
          let params = {
            page: this.page.pageIndex,
            limit: this.page.pageSize
          };
          this.init(params);
        })
        .catch(err => {
          this.$message.error(err);
        });
    },

    // 取消编辑
    handleDialogCancel(done) {
      /* this.$message({
        message: "取消编辑",
        type: "warning"
      }); */
      done();
    },

    // 生成批次
    createBatch({ index, row }) {
      this.$router.push({
        path: "/saleManage/orderInfo",
        query: {
          itemId: row.id,
        }
      });
    },

    // 删除分组
    delGood({ index, row }) {
      let params = {
        id: row.id,
      }
      this.$confirm(`此操作将删除缴费项目, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteItemBatch(params)
            .then(res => {
              if (res.success) {
                this.$message({
                  message: "删除成功",
                  type: "success"
                });
                // 重新加载列表
                let params = {
                  page: this.page.pageIndex,
                  limit: this.page.pageSize,
                };
                this.init(params);
              }
            })
            .catch(err => {
              this.$message.error(err);
            });
        })
        .catch(err => {});
    },
  }
};
</script>
<style lang="scss" scoped>
</style>
